// pages/find/index.js
Page({
  data: {
    menus: [
      // 图标来源：https://www.iconfont.cn/collections/detail?spm=a313x.7781069.1998910419.dc64b3430&cid=32622
      [
        {
          icon: "/assets/img/find/position.png",
          txt: "附近的人"
        },
        {
          icon: "/assets/img/find/group.png",
          txt: "发现群组"
        }
      ],
      [
        {
          icon: "/assets/img/find/live.png",
          txt: "城市直播"
        }
      ],
      [
        {
          icon: "/assets/img/find/sign.png",
          txt: "每日签到",
          rightTxt: "未签到"
        },
        {
          icon: "/assets/img/find/task.png",
          txt: "任务中心",
          rightTxt: "完成度12.5%"
        },
        {
          icon: "/assets/img/find/rank.png",
          txt: "城市排行榜"
        },
        {
          icon: "/assets/img/find/shop.png",
          txt: "本地商城"
        },
        {
          icon: "/assets/img/find/shop.png",
          txt: "热门推荐"
        }
      ]
    ]
  },

  onLoad: function (options) {},

  /**
   * 处理菜单点击（适配 tabBar 页面跳转）
   */
  onMenuTap: function(e) {
    const menu = e.detail
    
    if (!menu) {
      wx.showToast({
        title: '菜单项数据错误',
        icon: 'none'
      })
      return
    }
    
    // 配置路由及是否为 tabBar 页面
    const menuRoutes = {
      "附近的人": { url: "/pages/nearby/index", isTab: false },
      "发现群组": { url: "/pages/groups/index", isTab: false },
      "城市直播": { url: "/pages/live/index", isTab: false },
      "每日签到": { url: "/pages/checkin/index", isTab: false },
      "任务中心": { url: "/pages/task/index", isTab: false },
      "城市排行榜": { url: "/pages/rank/index", isTab: false },
      "本地商城": { url: "/pages/mall/index", isTab: false },
      "热门推荐": { url: "/pages/circle/index", isTab: true } // 标记为 tabBar 页面
    }
    
    const routeInfo = menuRoutes[menu.txt]
    if (routeInfo) {
      // 根据是否为 tabBar 页面选择跳转方式
      if (routeInfo.isTab) {
        wx.switchTab({
          url: routeInfo.url,
          fail: function(err) {
            console.error('tabBar页面跳转失败:', err)
            wx.showToast({
              title: '页面跳转失败',
              icon: 'none'
            })
          }
        })
      } else {
        wx.navigateTo({
          url: routeInfo.url,
          fail: function(err) {
            console.error('页面跳转失败:', err)
            wx.showToast({
              title: '页面跳转失败',
              icon: 'none'
            })
          }
        })
      }
    } else {
      wx.showToast({
        title: (menu.txt || '该') + '功能开发中',
        icon: 'none'
      })
    }
  }
})